// JavaScript Document
// require cookie:http://www.jq22.com/demo/jquery-cookies20160304/jquery.cookie.js
var $CART_URL = '/index';
var $CART_NAME = 'my_cart'; 
function getnum($v){ 
	if(isNaN($v)){
		$v=1;
	}else{
		$v=parseInt($v);
	}
	return $v;
}
(function () {
	$.getV = function ($v) {
		if ($v == undefined || $v == null) {
			return '';
		}
		return $v;
	}

	//0.获取购物车
	$.getCart = function () {
			var $cookie = $.cookie($CART_NAME);
			$cookie = $.getV($cookie);
			if ($cookie == '') {
				return '';
			} else {
				return $.parseJSON($cookie);
			}
		}
		//1.加入购物车
	$.addCart = function ($productid) {
			alertok('加入购物车成功');
			var $num = $.getV(arguments[1]);
			$num = ($num == '' ? 1 : $num);
			var $specification = $.getV(arguments[2]);
			var $url = $CART_URL + '/Cart/add?productid=' + $productid + "&specification=" + $specification + "&num=" + $num;
			$.ajax({
				url: $url,
				success: function (data) {
					if (data.code == 0) { 
						$.getCartNum();
					} else {
						alerterr(data.msg);
					}
				}
			});
		}
		//更新购物车
	$.editCart = function ($productid, $num) {
			//alertok('操作成功');
			var $specification = $.getV(arguments[2]);
			var $buynow = $.getV(arguments[3]);
			var $url = $CART_URL + '/Cart/edit?productid=' + $productid + "&specification=" + $specification + "&num=" + $num;
			$.ajax({
				url: $url,
				success: function (data) {
					if (data.code == 0) {
						if ($buynow == 1) {
							location = $CART_URL + '/settle/cart.html';
						}
					} else {
						alerterr(data.msg);
					}
				}
			});

		}
		//更新购物车
	$.checkCart = function ($productid, $specification, $checked) {
			//alertok('操作成功');
			var $url = $CART_URL + '/Cart/check?productid=' + $productid + '&checked=' + $checked + '&specification=' + $specification;
			$.ajax({
				url: $url,
				success: function (data) {
					if (data.code == 0) {} else {
						alerterr(data.msg);
					}
				}
			});

		}
		//删除购物车
	$.deleteCart = function ($productid) {
		alertok('移除成功');
		var $specification = $.getV(arguments[1]);
		var $iscart= $.getV(arguments[2]);
		var $url = $CART_URL + '/Cart/del?productid=' + $productid + "&specification=" + $specification;
		$.ajax({
			url: $url,
			success: function (data) {
				if (data.code == 0) {
					if($iscart==1){ 
						var $cart = $.getCart();
						var $num = $cart.cartnum; 
						$num = ($num ? $num:0); 
						if($num==0){
							location.reload();
						}
					}
				} else {
					alerterr(data.msg);
				}
			}
		});

	}


	//获取购物车信息
	$.getCartNum = function () {
		var $cart = $.getCart();
		var $num = $cart.cartnum; 
		$num = ($num ? $num:0);
		$("#cartnum").text($num);

	}

	//计算购物车已选数量和总额
	$.calcTotal = function () {
		var $count = 0,
			$total = 0;
		$(".cartchk:checked").each(function () {
			var itm = $(this);
			if (itm.prop('checked')) {
				var $price = parseFloat(itm.attr('data-price'));
				var $discount = parseFloat(itm.attr('data-discount'));
				var $num = parseInt(itm.parent().parent().parent().find('.cartnum').val()); 
				$count += $num;
				$total += ($num * ($price-$discount));
			}
		});
		if ($(".cartchk").not(':checked').length == 0) {
			$(".cartchkall").prop('checked', true);
		} else {
			$(".cartchkall").prop('checked', false);
		}
		$("#total").text($total.toFixed(2));
		$("#count").text($count);
		if($count>0){
			$("#btn-settle").removeClass("weui-btn_disabled");
		}else{
			$("#btn-settle").addClass("weui-btn_disabled");
		}

	}

	//载入购物车
	//	$.loadCart = function () {
	//		var $cart = $.getCart();
	//		//console.log($cart);
	//
	//		if ($cart != '') {
	//			var $items = $cart.items;
	//			var data = {
	//				list: $items
	//			};
	//			if ($("#tpl_cart").length > 0) {
	//				template.helper("parseInt", parseInt);
	//				template.helper("parseFloat", parseFloat);
	//				var html = template('tpl_cart', data);
	//				$("#mycartbox").html(html);
	//			}
	//			$("#cartnum").text(data.cartnum);
	//		} else {
	//			$("#mycartbox").html("<div class='noresult'><img src='/Public/index/images/cart_empty.png' style='width:50%; margin:20px 0px 40px;' /><br />购物车为空</div>");
	//			$("#cartnum").text(0);
	//		}
	//
	//	}

	//检查规格
	$.checkSpec = function () {
		var $select = "";
		var $ret = true;
		$(".specname").each(function (index, element) {
			var $chk = $(element).attr("data-id");
			if ($chk == "" || $chk == undefined) {
				$ret = false;
				return false;
			} else {
				$select += $chk + "_";
			}
		});
		var $checkedvalue='';
		$(".spec").each(function (index, element) {
			var $chk = $(element).prop("checked");
			var $value = $(element).attr("data-value");
			if ($chk) { 
				$checkedvalue += $value + "，";
			}
		});
		if ($ret) {
			$select = $select.substring(0, $select.length - 1);
			$checkedvalue = $checkedvalue.substring(0, $checkedvalue.length - 1);
			$("#product_num").attr("data-specification", $select);
			var $num=$("#product_num").val();
			$("#selected_spec").html(" "+$checkedvalue+" "+$num+"件");
			return $select;
		} else {
			return false;
		}
	}
})(jQuery);

$(function () {
	//	$.loadCart();
	$.getCartNum();

	//加入购物车
	$("body").on("click", ".addCart", function () {
		var $obj = $("#product_num");
		var $ismulti = $obj.attr("data-ismulti");
		var $id = $obj.attr("data-id");
		var $num = $obj.val();
		$num = (isNaN($num) ? 1 : parseInt($num));
		var $specification = $.getV($obj.attr("data-specification"));
		if ($ismulti) {
			//多规格
			$.addCart($id, $num, $specification);
		} else {
			$.addCart($id, $num, '');
		}
	});

	//立即购买
	$("body").on("click", ".buyCart", function () {
		var $obj = $("#product_num");
		var $ismulti = $obj.attr("data-ismulti");
		var $id = $obj.attr("data-id");
		var $num = $obj.val();
		$num = (isNaN($num) ? 1 : parseInt($num));
		var $specification = $.getV($obj.attr("data-specification"));
		if ($ismulti) {
			//多规格
			$.editCart($id, $num, $specification, 1);
		} else {
			$.editCart($id, $num, '', 1);
		} 
	});

	//立即购买
	$("body").on("change", ".cartnum", function () {
		var $obj = $(this);
		var $spec = $obj.attr("data-specification");
		var $id = $obj.attr("data-id");
		var $num = $obj.val();
		$num = (isNaN($num) ? 1 : parseInt($num));
		if($num<1){
			$num=1;
		}
		$obj.val($num);
		var $specification = $.getV($spec);
		$.editCart($id, $num, $spec);
		$.calcTotal();
	});

	//删除条目
	$("body").on("click", ".delCart", function () {
		var $obj = $(this);
		
		weui.confirm('您确定移除该商品吗？', function(){
		 	var $spec = $obj.attr("data-specification");
				var $id = $obj.attr("data-id");
				$obj.parent().parent().parent().remove();
				$.deleteCart($id, $spec,1);
		});
		 
		$.calcTotal();

	});
	
	//加
	$("body").on("click", ".btn-plus", function () {
		var $obj = $(this).prev(".cartnum");
		var $v=getnum($obj.val());
		$v++; 
		$obj.val($v).change();  
	});
	
	//减
	$("body").on("click", ".btn-minus", function () {
		var $obj = $(this).next(".cartnum");
		var $v=getnum($obj.val());
		$v--;
		if($v<1){
			$v=1;
		}
		$obj.val($v).change(); 
	});

	//勾选
	$("body").on("click", ".cartchk", function () {
		var $obj = $(this);
		var $checked = $obj.prop('checked');
		var $spec = $obj.attr("data-specification");
		var $id = $obj.attr("data-id"); 
		$.checkCart($id, $spec, ($checked ? 1 : 0));
		$.calcTotal();
	});
	$("body").on("click", ".cartchkall", function () {
		var $obj = $(this);
		var $checked = $obj.prop('checked');
		$(".cartchk").prop('checked', $checked);
		var $spec = $obj.attr("data-specification");
		var $id = $obj.attr("data-id");
		$.checkCart(-1, '', ($checked ? 1 : 0));
		$.calcTotal();
	});
	$("#btn-settle").click(function(){
		if($(this).hasClass("weui-btn_disabled")){
			return false;
		}
	});
	 
});
